package com.medical.lsq.controller;

import com.github.pagehelper.PageInfo;
import com.medical.lsq.entity.SysInstitution;
import com.medical.lsq.service.InstitutionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/institution")
public class InstitutionController {

    @Autowired
    private InstitutionService institutionService;
    /**
     * 获取医疗机构信息列表，包含模糊查询，分页
     * @param currentPage
     * @param limit
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/getInstitutionList")
    public String getInstitutionList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String instituName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysInstitution> pageInfo = institutionService.getInstitutionList(currentPage, limit, instituName);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", instituName);
        return "/institution/institution_list";
    }

    @RequestMapping("/openAdd")
    public String openadd() {return "/institution/institution_add";}

    @RequestMapping("/openEdit")
    public String openEdit(Integer id,Model model) {
        SysInstitution institution = institutionService.getInstiyution(id);

        model.addAttribute("institution", institution);
        return "/institution/institution_edit";
    }
    @RequestMapping("/add")
    public String add(SysInstitution sysInstitution,HttpServletRequest request, Model model){
        institutionService.add(sysInstitution);
        return getInstitutionList(1, 10, request, model);
    }

    @RequestMapping("edit")
    public String edit(SysInstitution institution, HttpServletRequest request, Model model) {
        institutionService.edit(institution);
        return getInstitutionList(1, 10, request, model);
    }

    @RequestMapping("delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        institutionService.delete(id);
        return getInstitutionList(1, 10, request,model);
    }

}
